#!/usr/bin/env python
#
#       tools.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os

import analyze
import handleargs

def printmod(modname,option,mod_dir=None):
    """
    Print function
    """
    print "Content Listing for Module: %s"%modname
    print "_______________________________________________________________"
    if option == 'all':
        print "Entity%35s%30s"%("Type","SubType")
        print "---------------------------------------------------------------"
        print_entity(mod_dir,type='listinlist')
    elif option == 'method':
        print "Entity%25s"%("Type: Method")
        print "---------------------------------------------------------------"
        print_entity(analyze.analyzemethod(modname),type='listinlist')
    elif option == 'class':
        print "Entity%25s"%("Type: Class")
        print "---------------------------------------------------------------"
        print_entity(analyze.analyzeclass(modname),type='list')
    else :
        print "Entity%25s"%("Type: Function")
        print "---------------------------------------------------------------"
        print_entity(analyze.analyzefunction(modname),type='list')
    print "---------------------------------------------------------------"

def print_entity(entitylist,type='listinlist'):
    """
    printing formats
    """
    if type == 'list':
        for entity in entitylist:
            print entity
    elif type == 'listinlist':
        max_sep=_getmaxpos(entitylist)+20
        for entity in entitylist:
            if len(entity) >= 3:
                max_sep1=_getmaxpos(entitylist,pos=1)+20
                print "%s%*s%*s"%(entity[0],max_sep-len(entity[0]),
                    entity[1],max_sep1-len(entity[1]),entity[2])
            else:
                print "%s%*s"%(entity[0],max_sep-len(entity[0]),entity[1])

def writemod(modname,option,content=None,fpath='pytestgen_contents.txt'):
    """
    Generate file for contents
    """
    fileobj = file(fpath,"w")
    if option == 'all':
        fileobj.write("Listing all Entities in module %s\n"%modname)
        fileobj.write("_______________________________\n")
        for index,subobj in enumerate(content):
            fileobj.write("%d. %s\n"%(index+1,subobj))
            map(lambda idx: fileobj.write("%s\n"%idx),\
                handleargs.getargs(modname,subobj))
            fileobj.write("\n")
    elif option == 'class':
        fileobj.write("Listing Classes in module %s\n"%modname)
        fileobj.write("_______________________________\n")
        for index,entity in enumerate(analyze.analyzeclass(modname)):
            fileobj.write("%d. %s\n"%(index+1,entity))
            map(lambda idx: fileobj.write("%s\n"%idx),\
                handleargs.getargs(modname,entitylist=[entity,'class']))
            fileobj.write("\n")
    elif option == 'method':
        fileobj.write("Listing Methods in module %s\n"%modname)
        fileobj.write("_______________________________\n")
        for index,entity in enumerate(analyze.analyzemethod(modname)):
            fileobj.write("%d. %s\n"%(index+1,entity))
            map(lambda idx: fileobj.write("%s\n"%idx),\
                handleargs.getargs(modname,[entity[0],'method',entity[1]]))
            fileobj.write("\n")
        fileobj.write("-------------------------------\n")
        fileobj.write("*The list is of the format [methodname,classname]\n")
    else :
        fileobj.write("Listing Functions in module %s\n"%modname)
        fileobj.write("_______________________________\n")
        for index,entity in enumerate(analyze.analyzefunction(modname)):
            fileobj.write("%d. %s\n"%(index+1,entity))
            map(lambda idx: fileobj.write("%s\n"%idx),handleargs.getargs\
                (modname,entitylist=[entity,'function']))
            fileobj.write("\n")
    fileobj.write("-------------------------------\n")
    fileobj.close()

def _getmaxpos(lists,pos=0):
    """
    return the maximum seperator
    """
    max_len=0
    for entity in lists:
        if len(entity[pos]) > max_len:
            max_len=len(entity[pos])

    return max_len
